﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Editor {
    public partial class WarehouseDataEdit : Form {
        public int WarehouseID = -1;    //仓库ID，默认为-1，便于判断
        public WarehouseDataEdit() {
            InitializeComponent();
        }

        private void WarehouseDataEdit_Load(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            if (WarehouseID != -1) {
                string sqlstr = $"select * from Warehouse where ID = {WarehouseID}";
                DataTable table = DBHelper.GetDataTable(sqlstr);
                textBox1.Text = WarehouseID.ToString();
                textBox2.Text = table.Rows[0][1].ToString();
                textBox3.Text = table.Rows[0][2].ToString();
                textBox4.Text = table.Rows[0][3].ToString();
                comboBox1.SelectedIndex = table.Rows[0][4].ToString().Equals("男") ? 0 : 1;
                textBox5.Text = table.Rows[0][5].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            Close();
        }

        private void button2_Click(object sender, EventArgs e) {
            if (textBox2.Text.Length * textBox3.Text.Length * textBox4.Text.Length * textBox5.Text.Length == 0) {
                MessageBox.Show("输入项不能为空!");
            } else {
                if (WarehouseID == -1) {
                    string sqlstr = $"insert Warehouse values('{textBox2.Text}','{textBox3.Text}','{textBox4.Text}',{comboBox1.SelectedItem},'{textBox5.Text}')";
                    if (DBHelper.ExecuteNonQuery(sqlstr)) {
                        MessageBox.Show("新增成功!");
                    } else {
                        MessageBox.Show("新增失败!");
                    }
                } else {
                    string sqlstr = $"Update Warehouse set Name = '{textBox2.Text}',Address = '{textBox3.Text}',Contact = '{textBox4.Text}',Gender = {(comboBox1.SelectedItem.Equals("男") ? "1" : "0")},Tel = '{textBox5.Text}'";
                    if (DBHelper.ExecuteNonQuery(sqlstr)) {
                        MessageBox.Show("编辑成功!");
                    } else {
                        MessageBox.Show("编辑失败!");
                    }
                }
            }
        }
    }
}
